// pages/subpackage/pages/crm/deviceReplace/deviceReplace.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device_old: '',//旧缴费器id
    device_no: '',//旧缴费器号
    user_name: '',//用户姓名
    user_tel: '',//用户电话

    device_new: '',//新缴费器号
    product: '',//新缴费器下发产品id
    product_name: '',
    product_type: '',//新缴费器下发产品类型
    type: '',//1智能缴费器2非智能缴费器
    package: '',//新缴费器下发套餐id
    package_name: '',
    note: '',//备注

    equipmentsList: [],//产品列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getProduct();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //选择缴费器
  navDeviceList() {
    wx.navigateTo({
      url: '../deviceManage/deviceManage?way=2',
    })
  },

  //获取产品
  getProduct(){
    var that = this;
    app.ajax({
      url: 'api/Equipments/getList',
      url_type: 2,
      data: {
        pageSize: 9999
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            equipmentsList: res.data.data
          })
        }
      }
    })
  },
  //选择下发的产品
  bindProduct(e) {
    var product_id = this.data.equipmentsList[e.detail.value].equipments_id;
    var product_name = this.data.equipmentsList[e.detail.value].equipments_name;
    var product_type = this.data.equipmentsList[e.detail.value].equipments_type;
    var type = this.data.equipmentsList[e.detail.value].type;
    this.setData({
      product: product_id,
      product_name: product_name,
      product_type: product_type,
      type: type,
    })
  },

  //新缴费器号输入
  devicenewInput(e){
    this.setData({
      device_new: e.detail.value
    })
  },
  //校验缴费器编号
  checkDevice(){
    var that = this;
    if (!that.data.device_new) {
      app.showToast('请输入新缴费器号');
      return
    }
    app.ajax({
      url: 'api/Equipmentlists/checkDevice',
      url_type: 2,
      data: {
        device_no: that.data.device_new
      },
      success: function (res) {
        app.showToast(res.data.msg);
      }
    })
  },

  //提交的表单数据
  formSubmit(e) {
    console.log('提交的数据', e)
    var that = this;
    var value = e.detail.value;
    if (!that.data.device_old) {
      app.showToast('请选择旧缴费器编号');
      return
    } else if (!value.device_new && that.data.type != 2) {
      app.showToast('请输入新缴费器编号');
      return
    } 
    var disable = false;
    if (disable){
      return
    }
    app.ajax({
      url: 'api/Equipmentlists/doReplaceDevice',
      url_type: 2,
      data: {
        device_one: that.data.device_old,
        device_two: value.device_new,
        product: that.data.product,
        note: value.remark
      },
      success: function (res) {
        disable = true;
        setTimeout(()=>{
          disable = false;
        },2000)
        app.showToast(res.data.msg, 'none', 2000, function () {
          if (res.data.code == 1000) {
            setTimeout(() => {
             // wx.navigateBack();
              wx.redirectTo({
                url: '/pages/subpackage/pages/crm/deviceEdit/deviceEdit?equipment_id=' + that.data.device_old,
              })
            }, 1000)
          }
        })
      }
    })
  },
  //表单重置
  formReset(){
    this.setData({
      device_old: '',//旧缴费器号
      user_name: '',//用户姓名
      user_tel: '',//用户电话
      device_new: '',//新缴费器号
      product: '',//新缴费器下发产品id
      product_name: '',
      package: '',//新缴费器下发套餐id
      package_name: '',
      note: '',//备注
    })
  },
})
